<template>
  <div>
    <el-checkbox-group v-model="checkListData.list" :disabled="stability"
      @change="(val)=>{handlGroup(val,checkListData.index)}">
      <ul class="ul">
        <li v-for="(item,index) in InvestigationList" :key="index + ';'">
          <el-checkbox :disabled="booleColor === -1" :label="item.id">{{item.inspect_type_name}}
          </el-checkbox>
        </li>
      </ul>
    </el-checkbox-group>
  </div>
</template>

<script>
import { Investigation } from "@/api/stability/giveUp";

export default {
  name: "inspection",
  props: {
    conditionsInve: {
      type: Array,
      default: function() {
        return [];
      }
    },
    booleColor: {
      type: Number,
      default: function() {
        return -1;
      }
    },
    stability: {
      type: Boolean,
      default: function() {
        return false;
      }
    }
  },
  data() {
    return {
      checkListData: {
        index: "",
        list: []
      },
      absIndex: 0,
      InvestigationList: []
    };
  },
  //是计算属性，也可以理解为一个方法。
  computed: {},

  //属性是手动定义的所需监听的值
  watch: {
    booleColor: {
      handler: function(newValue, oldValue) {
        if (newValue && newValue !== -1) {
          this.handlSampId();
        }
        if (newValue === -1) {
          this.checkListData.list = [];
        }
      },
      deep: true
    }
  },

  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.handlInvestigation();
  },

  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {},

  //是Vue实例对象上绑定的方法，供当前Vue组件作用域内使用，
  methods: {
    //获取考察类型
    handlInvestigation() {
      Investigation().then(res => {
        if (res.code === 200) {
          this.InvestigationList = res.data || [];
        }
      });
    },

    //样品id
    handlSampId() {
      let data = {};
      let ind = this.conditionsInve.findIndex(a => a.name == this.booleColor);
      if (ind !== -1) {
        this.tabsIndex = ind;
        data = this.conditionsInve[ind];
        this.checkListData = data;
      } else {
        this.checkListData.list = [];
      }
    },

    //考察类型勾选事件
    handlGroup(val, index) {
      this.handlSampId();
      let self = this;
      if (val.length > this.checkListData.conditions.length) {
        let label1 = "",
          name1 = "",
          inspect_type = {};
        for (let i = 0; i < val.length; i++) {
          let index33 = this.checkListData.conditions.findIndex(
            a => a.name === val[i]
          );
          if (index33 === -1) {
            let index44 = this.InvestigationList.findIndex(
              a => a.id === val[i]
            );
            if (index44 !== -1) {
              name1 = val[i];
              label1 = this.InvestigationList[index44].inspect_type_name;
              inspect_type = this.InvestigationList[index44];
            }
            break;
          }
        }
        let a = {
          label: label1,
          name: name1,
          list: [],
          conditions: []
        };
        let b = {
          inspect_type: inspect_type,
          place_conditions_infos: []
        };
        self.checkListData.conditions.push(a);
        self.checkListData.inspect_types.push(b);
      } else {
        let id2;
        for (let i = 0; i < this.checkListData.conditions.length; i++) {
          let index44 = val.findIndex(
            a => a == this.checkListData.conditions[i].name
          );
          if (index44 === -1) {
            id2 = i;
            break;
          }
        }
        self.checkListData.conditions.splice(id2, 1);
        self.checkListData.inspect_types.splice(id2, 1);
      }
      self.checkListData.list = val;
      this.handlSucess();
    },

    handlSucess: function() {
      this.$emit("handlSucess", this.tabsIndex);
    }
  }
};
</script>

<style scoped>
/* @import url(); 引入css类 */
.ul {
  margin: 0;
  padding: 0;
}

.ul li {
  height: 30px;
  line-height: 30px;
}

.ul li:hover {
  background-color: rgb(248, 248, 248);
  cursor: pointer;
}

.el-card {
  height: 260px;
}

::v-deep .el-tabs__item {
  width: 130px;
  text-align: center !important;
}
</style>
